/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 锁屏状态管理器测试
 */

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../lib/services/lockscreen_state_manager.dart';

void main() {
  group('LockscreenStateManager Tests', () {
    late LockscreenStateManager lockscreenStateManager;

    setUp(() {
      lockscreenStateManager = LockscreenStateManager.instance;
    });

    tearDown(() async {
      await lockscreenStateManager.dispose();
    });

    test('应该是单例模式', () {
      final instance1 = LockscreenStateManager.instance;
      final instance2 = LockscreenStateManager.instance;
      expect(instance1, same(instance2));
    });

    test('应该正确创建实例', () {
      expect(lockscreenStateManager, isNotNull);
      expect(lockscreenStateManager, isA<LockscreenStateManager>());
    });

    test('初始状态应该正确', () {
      expect(lockscreenStateManager.isLockscreen, false);
    });

    test('应该正确处理应用生命周期变化', () {
      // 模拟应用进入后台
      lockscreenStateManager
          .didChangeAppLifecycleState(AppLifecycleState.paused);
      expect(lockscreenStateManager.isLockscreen, true);

      // 模拟应用恢复前台
      lockscreenStateManager
          .didChangeAppLifecycleState(AppLifecycleState.resumed);
      expect(lockscreenStateManager.isLockscreen, false);
    });

    test('应该正确监听锁屏状态变化', () async {
      // 监听锁屏状态变化
      final stateChanges = <bool>[];
      final subscription =
          lockscreenStateManager.lockscreenStateStream.listen((isLockscreen) {
        stateChanges.add(isLockscreen);
      });

      // 触发状态变化
      lockscreenStateManager
          .didChangeAppLifecycleState(AppLifecycleState.paused);
      lockscreenStateManager
          .didChangeAppLifecycleState(AppLifecycleState.resumed);

      // 等待流事件
      await Future.delayed(const Duration(milliseconds: 100));

      // 验证状态变化
      expect(stateChanges, [true, false]);

      await subscription.cancel();
    });

    test('forceSyncToLockscreen 应该正常执行', () {
      expect(() => lockscreenStateManager.forceSyncToLockscreen(),
          returnsNormally);
    });

    test('checkAudioFocus 应该正常执行', () {
      expect(() => lockscreenStateManager.checkAudioFocus(), returnsNormally);
    });

    test('handleAudioFocusChange 应该正常执行', () {
      expect(() => lockscreenStateManager.handleAudioFocusChange(true),
          returnsNormally);
      expect(() => lockscreenStateManager.handleAudioFocusChange(false),
          returnsNormally);
    });

    test('dispose 应该正常执行', () async {
      expect(() => lockscreenStateManager.dispose(), returnsNormally);
    });
  });
}
